DOC import: lazy-read images

At least JPEG files are now only loaded when the user scrolls to the
relevant page.

Also fix the root cause of the EMF lazy-read problem and remove the
previous workarounds.

Change-Id: I9699927282b99bcb71a0d271a20bbfd56a361ee8
Reviewed-on: https://gerrit.libreoffice.org/53219
Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Tested-by: Jenkins <ci@libreoffice.org>
diff --git a/filter/source/msfilter/msdffimp.cxx b/filter/source/msfilter/msdffimp.cxx
index f2fbc02..3e80858 100644
--- a/filter/source/msfilter/msdffimp.cxx
+++ b/filter/source/msfilter/msdffimp.cxx
@@ -6545,7 +6545,14 @@
        else
        {   // and unleash our filter
            GraphicFilter& rGF = GraphicFilter::GetGraphicFilter();
            nRes = rGF.ImportGraphic( rData, "", *pGrStream );
            Graphic aGraphic = rGF.ImportUnloadedGraphic(*pGrStream);
            if (aGraphic)
            {
                rData = aGraphic;
                nRes = ERRCODE_NONE;
            }
            else
                nRes = rGF.ImportGraphic( rData, "", *pGrStream );

            // SJ: I40472, sometimes the aspect ratio (aMtfSize100) does not match and we get scaling problems,
            // then it is better to use the prefsize that is stored within the metafile. Bug #72846# for what the
diff --git a/oox/source/helper/graphichelper.cxx b/oox/source/helper/graphichelper.cxx
index 002d54b..073cfe6 100644
--- a/oox/source/helper/graphichelper.cxx
+++ b/oox/source/helper/graphichelper.cxx
@@ -244,7 +244,7 @@
        aArgs[ 1 ].Name = "LazyRead";
        aArgs[ 1 ].Value <<= true;

        if ( pExtHeader )
        if ( pExtHeader && pExtHeader->mapMode > 0 )
        {
            aArgs.realloc( aArgs.getLength() + 1 );
            Sequence< PropertyValue > aFilterData( 3 );
@@ -341,11 +341,6 @@
        EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find( rStreamName );
        if( aIt == maEmbeddedGraphics.end() )
        {
            // TODO make lazy-load work for EMF as well.
            WmfExternal aHeader;
            if (rStreamName.endsWith(".emf") && !pExtHeader)
                pExtHeader = &aHeader;

            xGraphic = importGraphic(mxStorage->openInputStream(rStreamName), pExtHeader);
            if( xGraphic.is() )
                maEmbeddedGraphics[ rStreamName ] = xGraphic;
diff --git a/sw/CppunitTest_sw_ww8import.mk b/sw/CppunitTest_sw_ww8import.mk
index 7e40c05..b710219 100644
--- a/sw/CppunitTest_sw_ww8import.mk
+++ b/sw/CppunitTest_sw_ww8import.mk
@@ -22,6 +22,7 @@
    sal \
    test \
    unotest \
    vcl \
    sfx \
    sw \
	utl \
diff --git a/sw/qa/extras/ww8import/data/image-lazy-read.doc b/sw/qa/extras/ww8import/data/image-lazy-read.doc
new file mode 100644
index 0000000..95017d2
--- /dev/null
+++ b/sw/qa/extras/ww8import/data/image-lazy-read.doc
Binary files differ
diff --git a/sw/qa/extras/ww8import/ww8import.cxx b/sw/qa/extras/ww8import/ww8import.cxx
index 907fe47..de16cfe 100644
--- a/sw/qa/extras/ww8import/ww8import.cxx
+++ b/sw/qa/extras/ww8import/ww8import.cxx
@@ -116,6 +116,14 @@
    CPPUNIT_ASSERT_EQUAL( sal_uInt16(0), pTableNd->GetTable().GetRowsToRepeat() );
}

DECLARE_OOXMLIMPORT_TEST(testImageLazyRead, "image-lazy-read.doc")
{
    auto xGraphic = getProperty<uno::Reference<graphic::XGraphic>>(getShape(1), "Graphic");
    Graphic aGraphic(xGraphic);
    // This failed, import loaded the graphic, it wasn't lazy-read.
    CPPUNIT_ASSERT(!aGraphic.isAvailable());
}

DECLARE_WW8IMPORT_TEST(testTdf106799, "tdf106799.doc")
{
    sal_Int32 const nCellWidths[3][4] = { { 9530, 0, 0, 0 },{ 2382, 2382, 2382, 2384 },{ 2382, 2382, 2382, 2384 } };
diff --git a/vcl/source/filter/graphicfilter.cxx b/vcl/source/filter/graphicfilter.cxx
index 023c07e..43feef3 100644
--- a/vcl/source/filter/graphicfilter.cxx
+++ b/vcl/source/filter/graphicfilter.cxx
@@ -1572,6 +1572,7 @@
            nGraphicContentSize = nStreamLength;
            pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);

            rIStream.Seek(nStreamBegin);
            rIStream.ReadBytes(pGraphicContent.get(), nStreamLength);

            if (!rIStream.GetError())
diff --git a/vcl/source/graphic/UnoGraphicProvider.cxx b/vcl/source/graphic/UnoGraphicProvider.cxx
index 8b9ccb9..7adb183 100644
--- a/vcl/source/graphic/UnoGraphicProvider.cxx
+++ b/vcl/source/graphic/UnoGraphicProvider.cxx
@@ -355,9 +355,6 @@
        }
    }

    if (bLazyRead && aFilterData.hasElements())
        bLazyRead = false;

    SolarMutexGuard g;

    if( xIStm.is() )
@@ -396,7 +393,10 @@
            aExtHeader.mapMode = nExtMapMode;
            WmfExternal *pExtHeader = nullptr;
            if ( nExtMapMode > 0 )
            {
                pExtHeader = &aExtHeader;
                bLazyRead = false;
            }

            ErrCode error = ERRCODE_NONE;
            if (bLazyRead)